package org.lq.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import org.lq.entity.Person;
import org.lq.service.PersonService;
import org.lq.service.impl.PersonServiceImpl;

import javax.swing.JTextPane;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PersonManger extends JFrame {

	private JPanel contentPane;
	private JTextField query_name;
	private static JTable table;
	private JTextField txt_name;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField txt_address;
	private PersonService ps = new PersonServiceImpl();
	private JRadioButton gender_m;
	private JComboBox txt_education;
	private JTextField txt_id;
	private JRadioButton gender_f;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		//以独立线程运行窗体组件
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PersonManger frame = new PersonManger();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PersonManger() {
		setTitle("人员管理");
		//设置关闭方式
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//625
		//430
		//指定坐标位置和大小
		setBounds(100, 100, 763, 430);
		//创建中间容器
		contentPane = new JPanel();
		//设置边框
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		//将中间容器添加到窗体中
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setHgap(23);
		panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 10, 727, 67);
		contentPane.add(panel);

		JLabel label = new JLabel("姓名:");
		panel.add(label);

		query_name = new JTextField();
		panel.add(query_name);
		query_name.setColumns(10);

		JLabel label_1 = new JLabel("性别:");
		panel.add(label_1);
		ButtonGroup genderGroup = new ButtonGroup();

		JRadioButton rdbtnAll = new JRadioButton("all");
		rdbtnAll.setSelected(true);
		panel.add(rdbtnAll);
		genderGroup.add(rdbtnAll);
		JRadioButton query_gender_m = new JRadioButton("男");
		query_gender_m.setSelected(true);
		panel.add(query_gender_m);

		JRadioButton query_gender_f = new JRadioButton("女");
		panel.add(query_gender_f);

		genderGroup.add(query_gender_m);
		genderGroup.add(query_gender_f);

		JLabel label_2 = new JLabel("学历:");
		panel.add(label_2);

		JComboBox query_education = new JComboBox();
		query_education.setModel(new DefaultComboBoxModel(new String[] {"所有", "博士", "硕士", "专科", "其他"}));
		panel.add(query_education);

		JButton button = new JButton("搜索");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = query_name.getText();
				String edu = query_education.getSelectedItem().toString();
				String gender = "";
				if(query_gender_f.isSelected()) {
					gender = "女";
				}else if(query_gender_m.isSelected()) {
					gender = "男";
				}else {
					gender = "";
				}
				if("所有".equals(edu)) {
					edu = "";
				}
				Person p = new Person();
				p.setName(name);
				p.setGender(gender);
				p.setEducation(edu);
				List<Person> list = ps.likeByPerson(p);
				initTable(list);


			}
		});
		panel.add(button);

		JButton button_1 = new JButton("添加");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				down();
			}
		});
		panel.add(button_1);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 87, 704, 296);
		contentPane.add(scrollPane);

		table = new JTable();
		table.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
						"\u7F16\u53F7", "\u59D3\u540D", "\u6027\u522B", "\u5B66\u5386", "\u5730\u5740"
				}
				));
		scrollPane.setViewportView(table);

		//		table.setRowSelectionInterval(1, 1);

		DefaultTableModel dtm = (DefaultTableModel) table.getModel();


		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);

		JMenuItem menuItem = new JMenuItem("显示详细信息");
		popupMenu.add(menuItem);

		JMenuItem menuItem_1 = new JMenuItem("删除");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//删除的单击事件
				//获取当前右键的行数
				//				System.out.println(table.getSelectedRow());
				int rowIndex = table.getSelectedRow();
				Object idstr = table.getValueAt(rowIndex, 0);
				int id = Integer.parseInt(idstr.toString());
				if(ps.delete(id)) {
					JOptionPane.showMessageDialog(PersonManger.this, "恭喜删除成功!","提示",JOptionPane.INFORMATION_MESSAGE);
					initTable();
				}else {
					JOptionPane.showMessageDialog(PersonManger.this, "删除失败!","错误",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		popupMenu.add(menuItem_1);

		JMenuItem menuItem_2 = new JMenuItem("修改");
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				down();
				//修改的单击事件
				int rowIndex = table.getSelectedRow();
				Object idstr = table.getValueAt(rowIndex, 0);
				Object namestr = table.getValueAt(rowIndex, 1);
				Object genderstr = table.getValueAt(rowIndex, 2);
				Object edustr = table.getValueAt(rowIndex, 3);
				Object addressstr = table.getValueAt(rowIndex, 4);

				txt_id.setText(idstr.toString());
				txt_name.setText(namestr.toString());
				txt_address.setText(addressstr.toString());
				//				System.out.println(genderstr);
				if("男".equals(genderstr)) {
					gender_m.setSelected(true);
				}else {
					gender_f.setSelected(true);
				}
				txt_education.setSelectedItem(edustr);

			}
		});
		popupMenu.add(menuItem_2);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "\u6570\u636E\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 393, 704, 181);
		contentPane.add(panel_1);
		panel_1.setLayout(null);

		JLabel label_3 = new JLabel("姓 名 : ");
		label_3.setBounds(238, 25, 54, 15);
		panel_1.add(label_3);

		txt_name = new JTextField();
		txt_name.setBounds(302, 22, 131, 21);
		panel_1.add(txt_name);
		txt_name.setColumns(10);

		JLabel label_4 = new JLabel("性 别 : ");
		label_4.setBounds(463, 28, 54, 15);
		panel_1.add(label_4);

		gender_m = new JRadioButton("男");
		gender_m.setSelected(true);
		buttonGroup.add(gender_m);
		gender_m.setBounds(523, 24, 54, 23);
		panel_1.add(gender_m);

		gender_f = new JRadioButton("女");
		buttonGroup.add(gender_f);
		gender_f.setBounds(579, 24, 54, 23);
		panel_1.add(gender_f);

		JLabel label_5 = new JLabel("学历 : ");
		label_5.setBounds(26, 77, 54, 15);
		panel_1.add(label_5);

		txt_education = new JComboBox();
		txt_education.setModel(new DefaultComboBoxModel(new String[] {"博士", "学士", "本科", "专科", "其他"}));
		txt_education.setBounds(90, 73, 131, 23);
		panel_1.add(txt_education);

		JLabel label_6 = new JLabel("地 址 :");
		label_6.setBounds(329, 77, 54, 15);
		panel_1.add(label_6);

		txt_address = new JTextField();
		txt_address.setBounds(389, 74, 131, 21);
		panel_1.add(txt_address);
		txt_address.setColumns(10);

		JButton btn_ok = new JButton("确定");
		btn_ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//添加操作
				String name = txt_name.getText();
				String address = txt_address.getText();
				String education = txt_education.getSelectedItem().toString();
				String gender = gender_m.isSelected() ? "男" : "女";
				String strid = txt_id.getText();

				Person p = new Person();
				p.setAddress(address);
				p.setEducation(education);
				p.setGender(gender);
				p.setName(name);

				if("".equals(txt_id.getText())) {
					if(ps.add(p)) {
						JOptionPane.showMessageDialog(PersonManger.this, "恭喜添加成功!","提示",JOptionPane.INFORMATION_MESSAGE);
						reset();
						up();
						//添加成功,将表格中的数据全部清空,然后重新查询数据库并添加到表格中
						initTable();
					}else {
						JOptionPane.showMessageDialog(PersonManger.this, "添加失败!","错误",JOptionPane.ERROR_MESSAGE);
					}
				}else {
					p.setId(Integer.parseInt(strid));
					if(ps.update(p)) {
						JOptionPane.showMessageDialog(PersonManger.this, "恭喜修改成功!","提示",JOptionPane.INFORMATION_MESSAGE);
						up();
						reset();
						initTable();
					}else {
						JOptionPane.showMessageDialog(PersonManger.this, "修改失败!","错误",JOptionPane.ERROR_MESSAGE);
					}
				}





			}
		});
		btn_ok.setBounds(147, 132, 93, 23);
		panel_1.add(btn_ok);

		JButton btn_cancel = new JButton("取消");
		btn_cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				up();
				reset();
			}
		});
		btn_cancel.setBounds(340, 132, 93, 23);
		panel_1.add(btn_cancel);

		JLabel label_7 = new JLabel("编 号 : ");
		label_7.setBounds(10, 25, 54, 15);
		panel_1.add(label_7);

		txt_id = new JTextField();
		txt_id.setEditable(false);
		txt_id.setBounds(90, 22, 66, 21);
		panel_1.add(txt_id);
		txt_id.setColumns(10);

		//初始化table中的数据
		initTable();
	}

	/**
	 * 生成右键菜单操作方法
	 * @param component
	 * @param popup
	 */
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				//显示菜单,当前鼠标在哪一行右键,哪一行就应该被选中
				//通过坐标获取右键的行数
				//				System.out.println(e.getPoint());
				int index = table.rowAtPoint(e.getPoint());
				//				System.out.println(index);
				popup.show(e.getComponent(), e.getX()+10, e.getY());
				table.setRowSelectionInterval(index, index);
			}
		});
	}
	public void initTable() {
		initTable(ps.findAll());
	}
	
	/**
	 * 初始化表格数据
	 */
	public void initTable(List<Person> list) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int count = dtm.getRowCount();
		for(int i = 0;i< count;i++) {
			dtm.removeRow(0);
		}
		//在添加数据之前,先清空表中的数据

		for (Person person : list) {
			dtm.addRow(new Object[] {
					person.getId(),
					person.getName(),
					person.getGender(),
					person.getEducation(),
					person.getAddress()
			});
		}
	}

	/**
	 * 重置
	 */
	public void reset() {
		txt_id.setText("");
		txt_address.setText("");
		txt_name.setText("");
		txt_address.setText("");
		gender_m.setSelected(true);

		txt_education.setSelectedIndex(0);
	}


	public void down() {
		int height = 430;
		while(height <=625) {
			height+=5;
			this.setSize(this.getWidth(), height);
		}
	}
	public void up() {
		int height = 625;
		while(height >=430) {
			height-=5;
			this.setSize(this.getWidth(), height);
		}
	}
}
